// comps/uploader/pic.js

const App = getApp()
import Qiniu from '../../utils/qiniu'

Component({
  /**
   * 组件的一些选项 
   */
  options: {
    addGlobalClass: true,
    multipleSlots: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    src: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: ''
    },
    height: {
      type: String,
      default: ''
    },
    readonly: {
      type: Boolean,
      default: false
    },
    base64: {
      type: Boolean,
      default: false
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    init: false,
    img: 'https://cdn.hangzhouyizuke.com/res/app/icons/ycimg.png',
    url: '',
    cancel: function () {},
    progress: {}
  },

  /**
   * 生命周期
   */
  lifetimes: {
    attached() {
      this.setData({
        init: true,
        url: this.data.src
      })
    },
    detached() {},
  },
  /**
   * 组件的方法列表
   */
  methods: {
    doDeleteImage() {
      this.setData({
        url: ''
      })
      let detail = {
        url: '',
        ocr: {}
      }
      this.triggerEvent('change', detail, {})
    },
    doPreviewImage() {
      wx.previewImage({
        urls: [this.data.url],
        current: this.data.url
      })
    },
    doUploadImage() {
      wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        sizeType: ['compressed'],
        camera: 'back',
        success: (res) => {
          this.execUpload(res.tempFiles[0].tempFilePath)
        }
      })
    },
    execUpload(path) {
      App.doPost({
        uri: '/comm/tool/file/qiniu/info',
        body: {
          type: 'Image'
        },
        fnSucc: resp => {
          let key = resp.data.path + resp.data.fileid + '.' + path.split('.').pop()?.toLowerCase()
          Qiniu.upload(path, (res) => {
            this.setData({
              url: res.imageURL
            })
            let b64 = this.data.base64 ? wx.getFileSystemManager().readFileSync(path, 'base64') : ""
            let detail = {
              url: res.imageURL,
              b64: b64,
            }
            this.triggerEvent('change', detail, {})
          }, (err) => {
            console.log(err)
          }, {
            region: 'NCN',
            key: key,
            uptoken: resp.data.token,
            uptokenFunc: null,
            uptokenURL: null,
            domain: resp.data.domain,
            shouldUseQiniuFileName: false,
          }, (progress) => {
            this.setData({
              progress
            })
            // console.log('上传进度', progress.progress);
            // console.log('已经上传的数据长度', progress.totalBytesSent);
            // console.log('预期需要上传的数据总长度', progress.totalBytesExpectedToSend);
          }, (cancel) => {
            this.setData({
              cancel
            })
          })
        },
        fnFail: resp => {
          App.doAlert('出错啦', `上传失败，[${resp.stat.err}] ${resp.stat.msg}`)
        }
      })
    },

  },

})